import { createStore } from '@/airstore';
import ApiClient from '@/services/apiClient';
import {
  transPlanStatus,
  transPreflight,
  transPostflight,
  transAirport,
} from './components/taskVFFormat';

function transformJobObj(spo) {
  return {
    id: spo.TS_TasktTid,
    acno: spo.MpDp_Acno,
    jobCard: spo.MpDp_JobCard,
    jobCardUrl: spo.MpDp_Card_Print_Ensure_Person,
    taskPaperType: spo.TS_TaskPaperType,
    cardTitle: spo.MpDp_CardTitle,
    workOrder: spo.MpDp_WorkOrder,
    worker: spo.R3,
    planDate: spo.MpDp_PlanDate,
    finishDate: spo.MpDp_Date,
    planStatusDes: transPlanStatus(spo.MpDp_PlanStatus),
    preflight: transPreflight(spo), //前序航班
    postflight: transPostflight(spo), //后序航班
    airport: transAirport(spo), //站点
    planStatus: spo.MpDp_PlanStatus,
    ifMain: spo.TS_IF_Main,
    orderType: spo.MpDp_OrderType,
    airportNext: spo.VF_DepartureAirport_Next,
    ifFeedbace: spo.MpDp_IfFeedbace,
    confirmCloseDDFC: spo.MpDp_ConfirmCloseDDFC,
    taskStageTid: spo.TS_TaskStageTid,
    flightTid: spo.MpDp_FlightTid,
    itemNo: spo.MpDp_Item,
    operatorId: spo.TS_TaskOperatorId,
  };
}

export default createStore({
  namespace: 'MaintainJob',
  finishTasks: [],
  list: [],
  setJobList(jbData) {
    if (jbData) {
      this.finishTasks = jbData[0].map(transformJobObj);
      this.list = jbData.slice(1).map((jbs) => jbs.map(transformJobObj));
    } else {
      this.finishTasks = [];
      this.list = [
        //[{ type: 1, id: '3096191363438002', acno: 'B-1676', airport: 'HRB' }],
      ];
    }
  },
  async getJobList(loginName) {
    const srRes = await ApiClient.post('mpController/v1/getTechTask', {
      loginUser: loginName,
      jsonResult: '',
    });
    this.setJobList(!srRes.errorCode ? srRes.data : null);
    return srRes;
  },
  //开始签收
  async taskSignIn(loginName, passPara) {
    const srRes = await ApiClient.post('mpController/v1/taskSignIn', {
      loginUser: loginName,
      jsonResult: JSON.stringify(passPara),
    });
    return srRes;
  },
  //任务关闭
  async closePlanJob(loginName, closePara) {
    const srRes = await ApiClient.post('mpController/v1/closePlanUpdateBase', {
      loginUser: loginName,
      jsonResult: JSON.stringify(closePara),
    });
    return srRes;
  },
  //任务关闭  MCO  不选择
  async closePackOfMco(loginName, closePara) {
    const srRes = await ApiClient.post('mpController/v1/closePackOfMco', {
      loginUser: loginName,
      jsonResult: JSON.stringify(closePara),
    });
    return srRes;
  },
  //任务关闭  MCO  选择-关闭保留
  async closePackCloseDfc(loginName, closePara) {
    const srRes = await ApiClient.post('mpController/v1/closePackCloseDfc', {
      loginUser: loginName,
      jsonResult: JSON.stringify(closePara),
    });
    return srRes;
  },
  //任务关闭  MCO  选择-不关闭保留
  async closePackNotCloseDfc(loginName, closePara) {
    const srRes = await ApiClient.post('mpController/v1/closePackNotCloseDfc', {
      loginUser: loginName,
      jsonResult: JSON.stringify(closePara),
    });
    return srRes;
  },
  //进出港到位
  async changeAdStatus(loginName, changeAdPara) {
    const srRes = await ApiClient.post('zoController/v1/changeAdStatus', {
      loginUser: loginName,
      jsonResult: JSON.stringify(changeAdPara),
    });
    return srRes;
  },
  //获取打分情况
  async getScoreDetails(loginName, taskStageTid) {
    const srRes = await ApiClient.post('zoController/v1/getScoreDetails', {
      loginUser: loginName,
      jsonResult: JSON.stringify(taskStageTid),
    });
    return srRes.data;
  },
  //保存打分情况
  async saveScoreDetails(loginName, scorePara) {
    const srRes = await ApiClient.post('zoController/v1/saveScoreDetails', {
      loginUser: loginName,
      jsonResult: JSON.stringify(scorePara),
    });
    return srRes;
  },
});
